// 导演类，控制游戏的逻辑
import {DataStore} from "./base/DataStore.js";
import {UpPencil} from "./runtime/UpPencil.js";
import {DownPencil} from "./runtime/DownPencil.js";

export class Director {
    constructor(){
        this.dataStore=DataStore.getInstance();
        this.moveSpeed=2;
    }
    createPencil(){
        const minTop=window.innerHeight/8;
        const maxTop=window.innerHeight/2;
        const top=minTop+Math.random()*(maxTop-minTop);
        this.dataStore.get("pencils").push(new UpPencil(top));
        this.dataStore.get("pencils").push(new DownPencil(top));
    }
    static getInstance(){
        if(!Director.instance){
            Director.instance=new Director();
        }
        return Director.instance;
    }
    isStrike(bird,pencil){
        return !(bird.top > pencil.bottom || bird.bottom < pencil.top || bird.right < pencil.left || bird.left > pencil.right);
    }
    check(){
        const birds=this.dataStore.get('birds');
        const land=this.dataStore.get('land');
        const pencils=this.dataStore.get("pencils");
        const score=this.dataStore.get("score");
        if(birds.birdsY[0]+birds.birdsHeight[0]>=land.y){
            console.log("游戏结束啦");
            this.isGameOver=true;
            return;
        }
        const birdsBorder={
            top:birds.y[0],
            bottom:birds.birdsY[0]+birds.birdsHeight[0],
            left:birds.birdsX[0],
            right:birds.birdsWidth[0]+birds.birdsX[0]
        };
        for(let i=0;i<pencils.length;i++){
            const pencil=pencils[i];
            const pencilBorder={
                top:pencil.y,
                bottom: pencil.y+pencil.height,
                left: pencil.x,
                right: pencil.x+pencil.width
            };
            if(this.isStrike(birdsBorder,pencilBorder)){
                this.isGameOver=true;
                return;
            }
        }
        //加分
        if(birds.birdsX[0]>pencils[0].x+pencils[0].width && score.isScore){
            score.isScore=false;
            score.scoreNumber+=1;
        }
    }
    run() {
        this.check();
        if (!this.isGameOver) {
            this.dataStore.get("background").draw();
            const pencils = this.dataStore.get("pencils");
            if (pencils.length === 4 && pencils[0].x + pencils[0].width <= 0) {
                pencils.shift();
                pencils.shift();
                this.dataStore.get("score").isScore=true;
            }
            if (pencils[0].x <= (window.innerWidth - pencils[0].width) / 2 && pencils.length === 2) {
                this.createPencil();
            }
            this.dataStore.get("pencils").forEach(function (value) {
                value.draw();
            });
            this.dataStore.get("land").draw();
            this.dataStore.get("score").draw();
            this.dataStore.get("birds").draw();
            let timer = requestAnimationFrame(() => this.run());
            this.dataStore.put("timer", timer);
        }else{
            this.dataStore.get("startButton").draw();
            cancelAnimationFrame(this.dataStore.get("timer"));
            this.dataStore.destory();
        }
    }
    birdsEvents(){
        for(let i=0;i<3;i++){
            this.dataStore.get("birds").y[i]=this.dataStore.get("birds").birdsY[i];
        }
        this.dataStore.get("birds").time=0;
    }
}